package cn.lj.logistics.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.tags.EditorAwareTag;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.lj.logistics.pojo.MsgObject;
import cn.lj.logistics.pojo.Role;
import cn.lj.logistics.pojo.RoleExample;
import cn.lj.logistics.pojo.Role;
import cn.lj.logistics.pojo.RoleExample;
import cn.lj.logistics.pojo.RoleExample.Criteria;
import cn.lj.logistics.pojo.User;
import cn.lj.logistics.pojo.UserExample;
import cn.lj.logistics.service.RoleService;
import cn.lj.logistics.service.UserService;
import cn.lj.logistics.service.RoleService;
import net.sf.jsqlparser.statement.delete.Delete;

@Controller
@RequestMapping("/role")
public class RoleController {
     
	@Autowired
	private RoleService roleService;
	
	@Autowired UserService userService;
	
	//编辑页面，新增和修改公用的页面
	@RequestMapping("/edit")
	public String edit(Model m,Long roleId) {
		
		//修改操作
		if (roleId!=null) {
			// 查询出当前用户的数据，共享给编辑页面，以供修改回显数据
			Role role = roleService.selectByPrimaryKey(roleId);
			m.addAttribute("role",role);
		}
	
		return "roleEdit";
		
	}
	

	
	//新增功能
		@RequestMapping("/insert")
		@ResponseBody
		public MsgObject insert(Role role) {
			
			
		
		int row = roleService.insert(role);
			MsgObject mo = MsgObject.createMO(0, "新增数据失败，请联系管理员！！！");
			if (row==1) {
				mo=MsgObject.createMO(1, "新增数据成功");
			}
			return mo;
			
		}
		
		//修改功能
				@RequestMapping("/update")
				@ResponseBody
				public MsgObject update(Role role) {
				
				
				int row = roleService.updateByPrimaryKeySelective(role);
					MsgObject mo = MsgObject.createMO(0, "修改数据失败，请联系管理员！！！");
					if (row==1) {
						mo=MsgObject.createMO(1, "修改数据成功");
					}
					return mo;
					
				}
		
	
	//删除功能
	@RequestMapping("/delete")
	@ResponseBody
	public MsgObject delete(Long roleId) {
		
		/*
		 * 删除前根据删除的角色id去管理员表查询是否还有管理员拥有这个角色
		 * 
		 * 有：不能删除
		 * 没有:可以删除
		 */
		UserExample example = new UserExample();
		cn.lj.logistics.pojo.UserExample.Criteria criteria = example.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		List<User> users = userService.selectByExample(example);

		MsgObject mo = MsgObject.createMO(0, "删除数据失败，请联系管理员！！");
		
		if(users.size() > 0) {
			mo = MsgObject.createMO(0, "此角色还拥有管理员，不能删除！");
			return mo;
		}
		
		int row = roleService.deleteByPrimaryKey(roleId);
	
		if (row==1) {
			mo=MsgObject.createMO(1, "删除数据成功");
		}
		return mo;
		
	}
	

	
	@RequestMapping("/rolePage")
	@RequiresPermissions("role:rolePage")
	public String rolePage() {
		
		return "rolePage";
		
	}
	

	@RequestMapping("/list")
	@RequiresPermissions("role:list")
	@ResponseBody
	public PageInfo<Role> list(@RequestParam(defaultValue = "1")Integer pageNum,
			               @RequestParam(defaultValue = "10")Integer pageSize ,String keyword){
		System.out.println("keyword:"+ keyword);
		//开始分页
		PageHelper.startPage(pageNum, pageSize);
		RoleExample example=new RoleExample();
		
		if (StringUtils.isNotBlank(keyword)) {
			// 创建限制对象
			Criteria criteria1 = example.createCriteria();
		criteria1.andRolenameLike("%"+keyword+"%"); 

	
		}
		List<Role> list = roleService.selectByExample(example);
		
		return  new PageInfo<Role>(list);
		
	}
}
